package com.nulldev.util.VariableAPI;

import com.nulldev.util.data.Variables;

public class BooleanUtil {
	public static String toBinary(final boolean value) {
		return (value ? "1" : "0");
	}

	public static boolean fromBinary(final String value) {
		return (Variables.requireNonNullObject(value, "value").equals("1"));
	}

	public static boolean fromBinary(final long value) {
		return (value == 1);
	}

	public static boolean safeParse(final String string) {
		if (string == null || string.isEmpty()) {
			return false;
		}
		return (string.equalsIgnoreCase("1") || string.equalsIgnoreCase("true") || string.equalsIgnoreCase("Boolean.TRUE")
				|| string.equalsIgnoreCase("true<newline>") || string.equalsIgnoreCase("YES"));
	}

	public static boolean safeParse(final String string, final boolean fallback) {
		if (string == null || string.isEmpty()) {
			return fallback;
		}
		return (string.equalsIgnoreCase("1") || string.equalsIgnoreCase("true") || string.equalsIgnoreCase("Boolean.TRUE")
				|| string.equalsIgnoreCase("true<newline>") || string.equalsIgnoreCase("YES"));
	}

	public static boolean random() {
		return (RandomUtil.randInt(-100, 100) > 0);
	}
}
